<template>
  <!-- 点赞 -->
  <van-button
    :icon="value === 1 ? 'good-job' : 'good-job-o'"
    :class="{
      liked: value === 1,
    }"
    :loading="loading"
    @click="onClik"
  />
</template>

<script>
import { addLike, deleteLike } from "@/api/article";
export default {
  name: "LickArticle",
  data() {
    return {
      loading: false,
    };
  },
  methods: {
    async onClik() {
      this.loading = true;
      try {
        let status = -1; //么有点赞状态
        if (this.value === 1) {
          // 已经点赞  取消
          await deleteLike(this.articleId);
        } else {
          // 未点赞  添加点赞
          await addLike(this.articleId);
          status = 1;
        }
        this.$emit("input", status);
        // 更新视图
        this.$toast.success(status === 1 ? "点赞成功" : "取消点赞");
      } catch (err) {
        console.log(err);
        this.$toast.fail("操作失败");
      }

      this.loading = false;
    },
  },
  props: {
    value: {
      type: Number,
      required: true,
    },
    articleId: {
      type: [Number, String, Object],
      required: true,
    },
  },
};
</script>

<style scoped lang="less">
.liked {
  .van-icon {
    color: #e5645f;
  }
}
</style>